﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Role.cs" company="Anton Frattaroli">
//   Copyright (c) Anton Frattaroli. All rights reserved.
// </copyright>
// <summary>
//   Defines the Role class.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace Lidocaine.Data
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.SqlClient;
    using System.Globalization;
    using Lidocaine.BusinessObjects;

    /// <summary>
    /// Defines the Role class.
    /// </summary>
    public static class Role
    {
        /// <summary>
        /// Gets the application roles.
        /// </summary>
        /// <returns>A read-only list of roles.</returns>
        public static List<BusinessObjects.Role> SelectAll()
        {
            BusinessObjects.Role role = null;
            List<BusinessObjects.Role> roles = new List<BusinessObjects.Role>();
            using (SqlConnection conn = new SqlConnection(Environment.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Lidocaine.GetRoles", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        if (rdr.HasRows)
                        {
                            while (rdr.Read())
                            {
                                role = new BusinessObjects.Role();
                                role.Id = Convert.ToInt32(rdr["RoleID"], CultureInfo.InvariantCulture);
                                role.Name = rdr["RoleName"].ToString();
                                roles.Add(role);
                            }
                        }
                    }
                }
            }

            return roles;
        }

        /// <summary>
        /// Selects all Role names for a Profile.
        /// </summary>
        /// <param name="profile">The member Profile.</param>
        /// <returns>A read-only list of Role names.</returns>
        public static List<string> SelectAllNames(BusinessObjects.UserProfile profile)
        {
            List<string> roles = new List<string>();
            using (SqlConnection conn = new SqlConnection(Environment.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Lidocaine.GetRoleNamesForUser", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@UserID", profile.Id);
                    using (SqlDataReader rdr = cmd.ExecuteReader())
                    {
                        if (rdr.HasRows)
                        {
                            while (rdr.Read())
                            {
                                roles.Add((string)rdr["RoleName"]);
                            }
                        }
                    }
                }
            }

            return roles;
        }

        public static void DeleteRoles(BusinessObjects.UserProfile profile)
        {
            using (SqlConnection conn = new SqlConnection(Environment.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Lidocaine.DeleteRolesForUser", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@UserID", profile.Id);
                    cmd.ExecuteNonQuery();
                }
            }
        }

        public static void AddUserToRole(BusinessObjects.UserProfile profile, BusinessObjects.Role role)
        {
            using (SqlConnection conn = new SqlConnection(Environment.ConnectionString))
            {
                conn.Open();
                using (SqlCommand cmd = new SqlCommand("Lidocaine.InsertUserInRole", conn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@UserID", profile.Id);
                    cmd.Parameters.AddWithValue("@RoleID", role.Id);
                    cmd.ExecuteNonQuery();
                }
            }
        }
    }
}